package com.zhuss.vent.processor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.vent.vent.wechat.DelTopicVentBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.vent.db.dao.ext.VentTopicDao;
import com.zhuss.util.JsonUtil;

/**
 * 删除评论匿名吐槽 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("ventDelTopic")
public class VentDelTopic extends AbstractProcessor<Object, DelTopicVentBo> implements Processor<Object, DelTopicVentBo> {

    @Autowired
	@Qualifier("ventRead")
	private VentRead ventRead;
    
    @Autowired
	@Qualifier("ventTopicRead")
	private VentTopicRead ventTopicRead;
    
    @Autowired
    private VentTopicDao ventTopicDao;
    
    /**
     * 校验
     */
    @Override
    public void check(DelTopicVentBo bo) throws WpsException {
    	
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(DelTopicVentBo param) throws WpsException {
    	Long ventTopicId = param.getVentTopicId();
    	List<Long> ventTopicIdList = param.getVentTopicIdList();
    	ventTopicIdList = new ArrayList<Long>();
    	ventTopicIdList.add(ventTopicId);
    	param.setVentTopicIdList(ventTopicIdList);
    	batchProcess(param);
    	param.setName(ventTopicId + "");
    	return null;
    }
    
    private void batchProcess(DelTopicVentBo param) throws WpsException {
    	List<Long> ventTopicIdList = param.getVentTopicIdList();
    	
    	if (ventTopicIdList == null || ventTopicIdList.isEmpty()) {
    		return;
    	}
    	
    	Integer deleted = param.getDeleted();
    	Long modifierId = param.getConcurrentUserId();
    	String modifier = param.getConcurrentUserName();
    	Date modifyTime = new Date();
    	
    	List<Long> ventTopicSonList = ventTopicRead.listSon(ventTopicIdList);
    	DelTopicVentBo sonBo = new DelTopicVentBo();
    	BeanUtils.copyProperties(param, sonBo);
    	if (ventTopicSonList != null && !ventTopicSonList.isEmpty()) {
    		sonBo.setVentTopicIdList(ventTopicSonList);
    	} else {
    		sonBo.setVentTopicIdList(null);
    	}
    	batchProcess(sonBo);
    	ventTopicDao.deleteByParentBatch(ventTopicIdList, deleted, modifierId, modifier, modifyTime);
    	ventTopicDao.deleteBatch(ventTopicIdList, deleted, modifierId, modifier, modifyTime);
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DelTopicVentBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.匿名吐槽.key());
    	record.setAction(LogActionEnum.匿名吐槽删除评论.key());
    	record.setTargetId(param.getVentTopicId());
    	record.setTargetName("评论内容：" + param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
